package com.wxtf.platform.main;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.wxtf.platform.tools.AccessDataBaseUtil;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.regex.Pattern;
import java.util.regex.Matcher;


/**
 * <li>Title: TaskExample.java</li>
 * <li>Project: baseplatform</li>
 * <li>Package: com.wxtf.platform.main</li>
 * <li>Description: </li>
 * <li>Copyright: Copyright (c) 2012</li>
 * <li>Company: wxthtf Technologies </li>
 * <li>Created on Sep 6, 2012 4:28:15 PM</li>
 *
 * @author chun_chang
 * @version 1.0.0.0
 *
 */
public class TaskExample extends Thread{
	private static final Log log = LogFactory.getLog(TaskExample.class);

	/** 
	 * 描述：init 做一些初始化工作
	 * @CreateOn Sep 6, 2012  4:39:32 PM
	 * @author chun_chang
	 */
	public void init() {
		this.setName("Task Example Thread");
		this.start();
	}

	@Override
	public void run() {
		while(true) {
			try {
				StringBuffer buff = new StringBuffer();
				buff.append("select e.org_id,e.tbe_id,e.tbe_name,e.tbe_sex,e.tbe_age,e.tbe_birthday,e.tbe_position,e.reserve1,o.tbeocr_time")
					.append(" from tb_employee e left join tb_family_base f on e.tbe_id=f.tbe_id ")
					.append(" left join tbe_only_child_record o on f.tbfi_id = o.tbfi_id")
					.append(" where f.tbfi_id in (select tbfi_id from tbe_only_child_record)");
	
				List<Object[]> list = AccessDataBaseUtil.query(buff.toString(), null);
				String sqlretire = "";
				if(list.size() > 0) {
					Date time1 = new Date();
					for(int i = 0; i < list.size(); i++) {// 行
						Object[] metaData = list.get(i);
						if(metaData[3].equals("女") && metaData[3] != "") {
							if(metaData[6].equals("普通职工") && metaData[6] != "") {
								if(remainDateToString(metaData[5].toString(), time1.toString(), 50)) {// 女职工
									sqlretire = sqlretire
											+ ","
											+ "insert into trp_only_child_retire_reward(trpocrr_id,org_id,tbe_id,trpocrr_name,trpocrr_sex,trpocrr_age,trpocrr_birthday,trpocrr_time,trpocrr_position,reserve1)values("
											+ "'" + AccessDataBaseUtil.getPKUUID() + "','" + metaData[0] + "','" + metaData[1] + "','" + metaData[2]
											+ "','" + metaData[3] + "''" + metaData[4] + "','" + metaData[8] + "','" + metaData[7] + "')";
								}
							} else {
								if(remainDateToString(metaData[5].toString(), time1.toString(), 55)) {// 女干部
									sqlretire = sqlretire
											+ ","
											+ "insert into trp_only_child_retire_reward(trpocrr_id,org_id,tbe_id,trpocrr_name,trpocrr_sex,trpocrr_age,trpocrr_birthday,trpocrr_time,trpocrr_position,reserve1)values("
											+ "'" + AccessDataBaseUtil.getPKUUID() + "','" + metaData[0] + "','" + metaData[1] + "','" + metaData[2]
											+ "','" + metaData[3] + "''" + metaData[4] + "','" + metaData[8] + "','" + metaData[7] + "')";
								}
							}
						} else {
							if(remainDateToString(metaData[5].toString(), time1.toString(), 60)) {// 男职工
								sqlretire = sqlretire
										+ ","
										+ "insert into trp_only_child_retire_reward(trpocrr_id,org_id,tbe_id,trpocrr_name,trpocrr_sex,trpocrr_age,trpocrr_birthday,trpocrr_time,trpocrr_position,reserve1)values("
										+ "'" + AccessDataBaseUtil.getPKUUID() + "','" + metaData[0] + "','" + metaData[1] + "','" + metaData[2]
										+ "','" + metaData[3] + "''" + metaData[4] + "','" + metaData[8] + "','" + metaData[7] + "')";
							}
						}
					}
				}
			
				//保存退休独生子女父母奖励记录
				String[] sqls = sqlretire.split(",");
				AccessDataBaseUtil.executeSQLUpdate(sqls);
				Thread.sleep(31 * 24 * 60 * 60 * 1000);// 停留31天
			} catch(Exception e) {
				log.error(e, e);
			}
		}
	}

	private static Calendar calS = Calendar.getInstance();
	private static Pattern p = Pattern.compile("\\d{4}-\\d{2}-\\d{2}");// 定义整则表达式

	/**
	 * 计算剩余时间
	 * 
	 * @param startDateStr
	 * @param endDateStr
	 * @return
	 */
	public static boolean remainDateToString(String startDateStr, String endDateStr, int age){
		java.util.Date startDate = null;
		java.util.Date endDate = null;
		try {
			startDate = new SimpleDateFormat("yyyy-MM-dd").parse(startDateStr);
		} catch (ParseException e) {
			e.printStackTrace();
			return false;
		}
		try {
			endDate = new SimpleDateFormat("yyyy-MM-dd").parse(endDateStr);
		} catch (ParseException e) {
			e.printStackTrace();
			return false;
		}
		calS.setTime(startDate);
		int startY = calS.get(Calendar.YEAR);
		int startM = calS.get(Calendar.MONTH);
		int startD = calS.get(Calendar.DATE);
		int startDayOfMonth = calS.getActualMaximum(Calendar.DAY_OF_MONTH);

		calS.setTime(endDate);
		int endY = calS.get(Calendar.YEAR);
		int endM = calS.get(Calendar.MONTH);
		// 处理2011-01-10到2011-01-10，认为服务为一天
		int endD = calS.get(Calendar.DATE) + 1;
		int endDayOfMonth = calS.getActualMaximum(Calendar.DAY_OF_MONTH);

		if(endDate.compareTo(startDate) < 0) {
			return false;
		}
		int lday = endD - startD;
		if(lday < 0) {
			endM = endM - 1;
			lday = startDayOfMonth + lday;
		}
		// 处理天数问题，如：2011-01-01 到 2013-12-31 2年11个月31天 实际上就是3年
		if(lday == endDayOfMonth) {
			endM = endM + 1;
			lday = 0;
		}
		int mos = (endY - startY) * 12 + (endM - startM);
		int lyear = mos / 12;
		if(lyear >= age) {
			return true;
		} else {
			return false;
		}
	}

	/*
	 * 转换 dataAndTime 2013-12-31 23:59:59 到 date 2013-12-31
	 */
	public static String getDate(String dateAndTime){
		if(dateAndTime != null && !"".equals(dateAndTime.trim())) {
			Matcher m = p.matcher(dateAndTime);
			if(m.find()) {
				return dateAndTime.subSequence(m.start(), m.end()).toString();
			}
		}
		return "data error";
	}
}
